package swingExtensions.component;

import io.github.jonestimd.swing.ComponentFactory;
import io.github.jonestimd.swing.component.*;
import io.github.jonestimd.swing.validation.Validator;
import io.github.jonestimd.text.StringFormat;
import net.miginfocom.swing.MigLayout;
import org.fluttercode.datafactory.impl.DataFactory;
import swingExtensions.demo.FrameDemo;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;

public class ComponentsDemo {

    private DataFactory factory = new DataFactory();
    private ComponentFactory componentFactory = new ComponentFactory();

    private AutosizeTextField autosizeTextField;
    private BeanListComboBox<String> nameComboBox;
    private CalendarPanel calendarPanel;
    private DateField dateField;
    private FileSuggestField fileSuggestField;
    private JTextField filterField;
    private MultiSelectField multiSelectField;
    private SuggestField<String> suggestField;
    private PredicateSuggestModel<String> suggestModel;

    public JPanel createContentPanel() {

        List<String> nameList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            nameList.add(factory.getFirstName());
        }

        JPanel panel = new JPanel(new MigLayout("fill, wrap 2", "[]rel[grow, fill]", ""));
        panel.add(new JLabel("AutosizeTextField"));
        panel.add(autosizeTextField = new AutosizeTextField(true));
        panel.add(new JLabel("BeanListComboBox"));
        panel.add(nameComboBox = BeanListComboBox.builder(nameList).get());
        panel.add(new JLabel("CalendarPanel"));
        panel.add(calendarPanel = new CalendarPanel(new Date()));
        panel.add(new JLabel("DateField"));
        panel.add(dateField = new DateField("yyyy-MM-dd"));
        try {
            fileSuggestField = new FileSuggestField(false, new File("./"));
            panel.add(new JLabel("FileSuggestField"));
            panel.add(fileSuggestField);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        filterField = componentFactory.newFilterField();
        panel.add(new JLabel("FilterField"));
        panel.add(filterField);
        multiSelectField = MultiSelectField.builder(true, false).items(nameList).get();
        panel.add(new JLabel("MultiSelectField"));
        panel.add(multiSelectField);
        String.format("%s, %s", "", "");
        suggestModel = new PredicateSuggestModel<>((item, text) -> {
            System.out.println(String.format("item: %s, text: %s", item, text));
            return item.toLowerCase().startsWith(text.toLowerCase());
        }, String::compareToIgnoreCase);
        suggestModel.setElements(nameList, false);
        suggestField = new SuggestField<>(new StringFormat(), Validator.empty(), suggestModel);
        panel.add(new JLabel("SuggestField"));
        panel.add(suggestField);
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ComponentsDemo demo = new ComponentsDemo();
            FrameDemo frameDemo = new FrameDemo(ComponentsDemo.class.toString());
            frameDemo.setContentPane(demo.createContentPanel());
            frameDemo.setVisible(true);
        });
    }
}
